<?php
namespace app\admin\validate;

use think\Validate;
use app\admin\model\Appin as AM;

class AppinValidate extends Validate
{
    protected $rule = [
        'tag' => 'checkTag:tag'
    ];

    protected $message = [
        'tag.checkTag' => '应用tag_name错误或者应用已存在'
    ];

    protected $scene = [
        'install' => ['tag']
    ];

    public function checkTag($value, $rule, $data)
    {
		$appin = AM::where('tag_name', $value)->value('id');
		if ( isset($data['act']) && $data['act'] === 'reset') {
			if ( !$appin ) {
				throw new \Exception('应用不存在');
			} else {
				return true;
			}
		} else {
			if ($appin) {
			    return false;
			} else {
			    return true;
			}
		}
        
    }
}
